package com.oscar.ehome.view.pageview.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.base.C;
import com.oscar.ehome.bean.messageBean.MessageListBean;
import com.oscar.ehome.bean.pagebean.HdBean;
import com.oscar.ehome.bean.pagebean.PageListBean;
import com.oscar.ehome.bean.pagebean.StudyBean;
import com.oscar.ehome.provider.gildeimage.GildeUtils;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.DateUtil;
import com.oscar.ehome.view.messageview.adapter.MessageListAdapter;

import org.apache.commons.lang.StringUtils;

import java.util.List;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/7/25
 */

public class PageListAdapter extends BaseAdapter {

    private List<PageListBean.PageBean> specailList;
    private LayoutInflater mInflater;
    private Context mContext = null;

    StudyBean.ListBean learn;
    HdBean.ListBean event;
    MessageListBean oaNotify;

    String isSticky = "";
    String type = "";
    String date = "";

    public PageListAdapter(Context context, List<PageListBean.PageBean> specailList) {
        this.mContext = context;
        this.specailList = specailList;
        mInflater = LayoutInflater.from(context);
    }

    public void setLists(List<PageListBean.PageBean> specailList) {
        this.specailList = specailList;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return specailList.size();
    }

    @Override
    public Object getItem(int position) {
        return specailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_pagelist, null);
            holder = new ViewHolder();
            holder.page_hd_layout = (LinearLayout) convertView.findViewById(R.id.page_hd_layout);//纯图片布局
            holder.page_img = (ImageView) convertView.findViewById(R.id.page_img);
            holder.page_type = (ImageView) convertView.findViewById(R.id.page_type);
            holder.page_hd_title = (TextView) convertView.findViewById(R.id.page_hd_title);
            holder.page_hd_time = (TextView) convertView.findViewById(R.id.page_hd_time);
            holder.page_hd_stick = (TextView) convertView.findViewById(R.id.page_hd_stick);
            holder.page_hd_creattime = (TextView) convertView.findViewById(R.id.page_hd_creattime);

            holder.page_study_layout = (RelativeLayout) convertView.findViewById(R.id.page_study_layout);//消息类型布局
            holder.page_study_title = (TextView) convertView.findViewById(R.id.page_study_title);
            holder.page_study_img = (ImageView) convertView.findViewById(R.id.page_study_img);
            holder.page_types = (ImageView) convertView.findViewById(R.id.page_types);

            holder.page_title_layout = (RelativeLayout) convertView.findViewById(R.id.page_title_layout);//有置顶的布局
            holder.page_stick = (TextView) convertView.findViewById(R.id.page_stick);
            holder.page_titles = (TextView) convertView.findViewById(R.id.page_titles);
            holder.page_time = (TextView) convertView.findViewById(R.id.page_time);

            holder.page_title_layouts = (RelativeLayout) convertView.findViewById(R.id.page_title_layouts);//无置顶的布局
            holder.page_title_text = (TextView) convertView.findViewById(R.id.page_title_text);
            holder.page_time_text = (TextView) convertView.findViewById(R.id.page_time_text);

            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }

        if (specailList.size() >= 0 || specailList != null) {
            PageListBean.PageBean bean = specailList.get(position);
            if (bean.getCreateDate() != null) {//时间
                date = DateUtil.timeLogic(bean.getCreateDate());
            } else {
                date = "";
            }
            String type_name = bean.getfTableName();//类型
            if (type_name != null) {
                switch (type_name) {
                    case "learn"://学习
                        learn = bean.getLearn();
                        isSticky = learn.getIsSticky();
                        holder.page_hd_layout.setVisibility(View.GONE);
                        holder.page_study_layout.setVisibility(View.VISIBLE);
                        if (learn.getTitle() != null) {
                            holder.page_study_title.setText(learn.getTitle());
                        }
                        if ("1".equals(isSticky)) {
                            holder.page_title_layout.setVisibility(View.VISIBLE);
                            holder.page_title_layouts.setVisibility(View.GONE);
                            holder.page_time.setText(date);
                        } else {
                            holder.page_title_layout.setVisibility(View.GONE);
                            holder.page_title_layouts.setVisibility(View.VISIBLE);
                            holder.page_time_text.setText(date);
                        }
                        if (learn.getSmallImg() != null) {
                            String learnType = learn.getLearnType();
                            if ("video/mp4".equals(learnType)) {//视频
                                GildeUtils.loadUrlImageshipin(C.qiniu_ip + learn.getSmallImg(), holder.page_study_img, mContext);
                            } else if ("application/pdf".equals(learnType)) {//PDF
                                GildeUtils.loadUrlImagepdf(C.qiniu_ip + learn.getSmallImg(), holder.page_study_img, mContext);
                            } else if ("audio/mp3".equals(learnType)) {//音频
                                GildeUtils.loadUrlImageyinpin(C.qiniu_ip + learn.getSmallImg(), holder.page_study_img, mContext);
                            }

                        }
                        holder.page_types.setImageResource(R.mipmap.bianjiaoxx);
                        break;
                    case "event"://活动
                        event = bean.getEvent();
                        isSticky = event.getIsSticky();
                        if (StringUtils.isNotEmpty(event.getEventBigimg())) {


                            holder.page_hd_layout.setVisibility(View.VISIBLE);
                            holder.page_study_layout.setVisibility(View.GONE);
                            if (event.getTtitle() != null) {
                                holder.page_hd_title.setText(event.getTtitle());
                            }
                            if (event.getStartDate() != null) {
                                holder.page_hd_time.setText(event.getStartDate());
                            }
                            holder.page_hd_creattime.setText(date);
                        } else {
                            if (StringUtils.isNotEmpty(event.getEventImg())
                                    || StringUtils.isNotEmpty(event.getEventDefaultimg())) {
                                holder.page_hd_layout.setVisibility(View.GONE);
                                holder.page_study_layout.setVisibility(View.VISIBLE);
                                if (event.getTtitle() != null) {
                                    holder.page_study_title.setText(event.getTtitle());
                                }
                                if ("1".equals(isSticky)) {
                                    holder.page_title_layout.setVisibility(View.VISIBLE);
                                    holder.page_title_layouts.setVisibility(View.GONE);
                                    holder.page_time.setText(date);
                                } else {
                                    holder.page_title_layout.setVisibility(View.GONE);
                                    holder.page_title_layouts.setVisibility(View.VISIBLE);
                                    holder.page_time_text.setText(date);
                                }
                                holder.page_types.setImageResource(R.mipmap.bianjiaohd);
                            }
                        }

                        Log.e("tiupian", "getView: " + event.getEventBigimg() + "    " + event.getEventImg() + "   " + event.getEventDefaultimg());


                        if ("1".equals(isSticky)) {
                            holder.page_hd_stick.setVisibility(View.VISIBLE);
                        } else {
                            holder.page_hd_stick.setVisibility(View.GONE);
                        }

                        if (StringUtils.isNotEmpty(event.getEventBigimg())) {
                            holder.page_hd_layout.setVisibility(View.VISIBLE);
                            holder.page_study_layout.setVisibility(View.GONE);
                            GildeUtils.loadUrlImages(C.qiniu_ip + event.getEventBigimg(), holder.page_img, mContext);
                            Log.e("tiupian", "getView: " + C.qiniu_ip + event.getEventBigimg());
                            break;
                        }
                        if (StringUtils.isNotEmpty(event.getEventImg())) {
                            holder.page_hd_layout.setVisibility(View.GONE);
                            holder.page_study_layout.setVisibility(View.VISIBLE);
                            GildeUtils.loadUrlImages(C.qiniu_ip + event.getEventImg(), holder.page_study_img, mContext);
                            Log.e("tiupian123", "getView: " + C.qiniu_ip + event.getEventImg());
                            break;
                        }
                        if (StringUtils.isNotEmpty(event.getEventDefaultimg())) {
                            holder.page_hd_layout.setVisibility(View.GONE);
                            holder.page_study_layout.setVisibility(View.VISIBLE);
                            GildeUtils.loadUrlImages(C.qiniu_ip + event.getEventDefaultimg(), holder.page_study_img, mContext);
                            Log.e("tiupian456", "getView: " + C.qiniu_ip + event.getEventDefaultimg());
                            break;
                        }
                        break;

                    case "oaNotify"://消息
                        oaNotify = bean.getOaNotify();
                        isSticky = oaNotify.getIsSticky();
                        type = oaNotify.getType();
                        holder.page_hd_layout.setVisibility(View.GONE);
                        holder.page_study_layout.setVisibility(View.VISIBLE);
                        if (oaNotify.getTitle() != null) {
                            holder.page_study_title.setText(oaNotify.getTitle());
                        }
                        if (type != null) {
                            type_view(type, holder, 3);
                            holder.page_stick.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                            TintManager.setTint(holder.page_stick, Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));

                        }
                        holder.page_types.setImageResource(R.mipmap.bianjiaotz);
                        break;
                }
            }
        }
        return convertView;
    }

    public void type_view(String type, ViewHolder holder, int typename) {
        switch (type) {
            case "1"://通知
                if (typename == 3) {
                    holder.page_study_img.setImageResource(R.mipmap.tongzhi1);
                }
                if ("1".equals(isSticky)) {
                    holder.page_title_layout.setVisibility(View.VISIBLE);
                    holder.page_title_layouts.setVisibility(View.GONE);
                    holder.page_titles.setText(R.string.message_inform);
                    holder.page_time.setText(date);
                } else {
                    holder.page_title_layout.setVisibility(View.GONE);
                    holder.page_title_layouts.setVisibility(View.VISIBLE);
                    holder.page_title_text.setText(R.string.message_inform);
                    holder.page_time_text.setText(date);
                }
                holder.page_titles.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                break;
            case "2"://激励
                if (typename == 3) {
                    holder.page_study_img.setImageResource(R.mipmap.jili1);
                }
                if ("1".equals(isSticky)) {
                    holder.page_title_layout.setVisibility(View.VISIBLE);
                    holder.page_title_layouts.setVisibility(View.GONE);
                    holder.page_titles.setText(R.string.message_exciting);
                    holder.page_time.setText(date);
                } else {
                    holder.page_title_layout.setVisibility(View.GONE);
                    holder.page_title_layouts.setVisibility(View.VISIBLE);
                    holder.page_title_text.setText(R.string.message_exciting);
                    holder.page_time_text.setText(date);
                }
                holder.page_titles.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                break;
            case "3"://产品
                if (typename == 3) {
                    holder.page_study_img.setImageResource(R.mipmap.chanpin1);
                }
                if ("1".equals(isSticky)) {
                    holder.page_title_layout.setVisibility(View.VISIBLE);
                    holder.page_title_layouts.setVisibility(View.GONE);
                    holder.page_titles.setText(R.string.message_product);
                    holder.page_time.setText(date);
                } else {
                    holder.page_title_layout.setVisibility(View.GONE);
                    holder.page_title_layouts.setVisibility(View.VISIBLE);
                    holder.page_title_text.setText(R.string.message_product);
                    holder.page_time_text.setText(date);
                }
                holder.page_titles.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                break;
            case "4"://会议
                if (typename == 3) {
                    holder.page_study_img.setImageResource(R.mipmap.huiyi1);
                }
                if ("1".equals(isSticky)) {
                    holder.page_title_layout.setVisibility(View.VISIBLE);
                    holder.page_title_layouts.setVisibility(View.GONE);
                    holder.page_titles.setText(R.string.message_meeting);
                    holder.page_time.setText(date);
                } else {
                    holder.page_title_layout.setVisibility(View.GONE);
                    holder.page_title_layouts.setVisibility(View.VISIBLE);
                    holder.page_title_text.setText(R.string.message_meeting);
                    holder.page_time_text.setText(date);
                }
                holder.page_titles.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                break;
            case "5"://系统
                if (typename == 3) {
                    holder.page_study_img.setImageResource(R.mipmap.xitong1);
                }
                if ("1".equals(isSticky)) {
                    holder.page_title_layout.setVisibility(View.VISIBLE);
                    holder.page_title_layouts.setVisibility(View.GONE);
                    holder.page_titles.setText(R.string.message_system);
                    holder.page_time.setText(date);
                } else {
                    holder.page_title_layout.setVisibility(View.GONE);
                    holder.page_title_layouts.setVisibility(View.VISIBLE);
                    holder.page_title_text.setText(R.string.message_system);
                    holder.page_time_text.setText(date);
                }
                holder.page_titles.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
                break;
        }
    }


    class ViewHolder {
        ImageView page_types;
        ImageView page_study_img;
        TextView page_study_title;
        RelativeLayout page_study_layout;//title

        RelativeLayout page_title_layout;//有置顶的布局
        TextView page_stick;
        TextView page_titles;
        TextView page_time;

        RelativeLayout page_title_layouts;//无置顶的布局
        TextView page_title_text;
        TextView page_time_text;

        LinearLayout page_hd_layout;//活动大图布局
        ImageView page_img;
        ImageView page_type;
        TextView page_hd_title;
        TextView page_hd_time;
        TextView page_hd_stick;
        TextView page_hd_creattime;
    }

}
